package com.soilflow.web.controller.soilflow;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soilflow.common.domain.LandHire;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.domain.dto.LandHireDto;
import com.soilflow.info.service.ILandHireService;
import com.soilflow.info.service.LandInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.soilflow.common.annotation.Log;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.poi.ExcelUtil;
import com.soilflow.common.core.page.TableDataInfo;

/**
 * 【租赁管理】Controller
 * 
 * @author party
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/land/hire")
public class LandHireController extends BaseController {
    @Autowired
    private ILandHireService landHireService;
    @Autowired
    private LandInfoService landInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LandHireDto landHire) {
        startPage();
        List<LandHire> list = landHireService.selectLandHireList(landHire);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:hire:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LandHireDto landHire)
    {
        List<LandHire> list = landHireService.selectLandHireList(landHire);
        ExcelUtil<LandHire> util = new ExcelUtil<LandHire>(LandHire.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:hire:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(landHireService.selectLandHireById(id));
    }

    /**
     * 结束租赁信息
     * @param id
     * @return
     */
    @GetMapping(value = "/endHire/{id}")
    public AjaxResult endHire(@PathVariable("id") Long id)
    {
        // 根据土地id结束租赁
        LandInfo byId = landInfoService.getById(id);
        byId.setStatus("0");
        landInfoService.updateById(byId);
        return AjaxResult.success();
    }
    /**
     * 查询土地信息
     */
    @GetMapping("/selectinfo")
    public TableDataInfo selectinfo(){
        startPage();
        LambdaQueryWrapper<LandInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(LandInfo::getDeleted,0);
        List<LandInfo> list = landInfoService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 新增【租赁信息】，同时新增租金信息
     */
//    @PreAuthorize("@ss.hasPermi('system:hire:add')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LandHire landHire) {
        return toAjax(landHireService.insertLandHire(landHire));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:hire:edit')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/updateHire")
    public AjaxResult edit(@RequestBody LandHire landHire)
    {
        return toAjax(landHireService.updateLandHire(landHire));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:hire:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(landHireService.deleteLandHireByIds(ids));
    }
    /**
     * 按年对土地租赁进行统计
     */
    @GetMapping("/statistics")
    public AjaxResult statistics(){
        return AjaxResult.success(landHireService.statistics());
    }
    /**
     * 按村对土地租赁进行统计
     */
    @GetMapping("/village")
    public AjaxResult village(){
        return AjaxResult.success(landHireService.village());
    }
    /**
     * 统计今年土地租赁数量
     */
    @GetMapping("/thisyear")
    public AjaxResult thisyear(){
        return AjaxResult.success(landHireService.thisyear());
    }
}
